/**
 * Copyright 2011, Under-Ground Studio.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zju.vlis.aus.chart;

import cn.edu.zju.vlis.aus.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * Instance of this class is a horizontal bar in the bar chart item to represent
 * the data.
 * 
 * @author ymsong (songyuming1985@gmail.com) 2011-9-17
 */
public class BarView extends View {
	// current animation percent, range[0, 1]
	static float animationPercent = 1;
	static int maxCount = 100;
	static Rect textBounds;

	private int barHeight = 20;
	private int textSpacing = 10;
	private float textSize = 15;
	private DisplayMetrics metrics;
	private String packageName;
	private int count;

	public BarView(Context context) {
		super(context);
		initMetrics();
	}

	public BarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initMetrics();
	}

	private void initMetrics() {
		metrics = new DisplayMetrics();
		Activity activity = (Activity) getContext();
		activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
		barHeight *= metrics.scaledDensity;
		textSpacing *= metrics.scaledDensity;
		textSize *= metrics.scaledDensity;
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int width = getWidth();
		int height = getHeight();
		int paddingLeft = getPaddingLeft();
		int paddingRight = getPaddingRight();
		int paddingTop = getPaddingTop();
		int paddingBottom = getPaddingBottom();
		Paint paint = new Paint();
		paint.setColor(0xFF000000 | packageName.hashCode());
		paint.setAntiAlias(true);
		paint.setTextSize(textSize);
		// measure text
		if (textBounds == null) {
			String maxCountStr = String.valueOf(maxCount);
			textBounds = new Rect();
			paint.getTextBounds(maxCountStr, 0, maxCountStr.length(),
					textBounds);
		}

		// draw bar.
		float barWidth = width - paddingLeft - paddingRight - textSpacing
				- textBounds.right;
		float factor = (float) count / (float) maxCount * animationPercent;
		barWidth *= factor;
		if (barWidth < 10) {
			barWidth = 10;
		}
		int barHeight = height - paddingBottom - paddingTop;
		canvas.drawRect(paddingLeft, paddingTop, barWidth, barHeight, paint);
		Drawable mask = getContext().getResources().getDrawable(
				R.drawable.mask_barchart);
		mask.setBounds(paddingLeft, paddingTop, (int) barWidth + 2,
				barHeight + 2);
		mask.draw(canvas);

		// draw count.
		paint.setColor(Color.BLACK);
		canvas.drawText(String.valueOf(count), paddingLeft + barWidth
				+ textSpacing, paddingTop + (barHeight - textBounds.top) / 2,
				paint);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(measureWidth(widthMeasureSpec),
				measureHeight(heightMeasureSpec));
	}

	private int measureWidth(int widthMeasureSpec) {
		int width = 0;
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		if (widthMode == MeasureSpec.EXACTLY) {
			width = widthSize;
		}
		return width;
	}

	private int measureHeight(int heightMeasureSpec) {
		int height = 0;
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		if (heightMode == MeasureSpec.EXACTLY) {
			height = heightSize;
		} else {
			height = (int) barHeight;
		}
		return height;
	}

	public static void increaseAnimationPercent() {
		if (animationPercent < 1.0f) {
			animationPercent += 0.02f;
		}
		if (animationPercent > 1.0) {
			animationPercent = 1.0f;
		}
	}
}